/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>


#include "smSetup.h"
#include "../tools/log.h"
#include <string.h>
#include "factories.h"
#include "AbstractLp.h"
#include "submodelManager.h"
#include "corbaMessageHandler.h"



CORBA::Boolean smSetup_i::createLP(CORBA::ULong id, const char* neighbors, const char* MeName, const char* LPtype, const char* Adapatername)
{
    FILE_LOG(logDEBUG)<<"createLP called: "<<id<<"|"<<neighbors<<"|"<<MeName<<"|"<<LPtype<<"|"<<Adapatername;
    vector<unsigned int> myneighbors;

vector<string> tokens;

istringstream iss(neighbors);
copy(istream_iterator<string>(iss),
         istream_iterator<string>(),
         back_inserter<vector<string> >(tokens));
  for (int i=0;i<tokens.size();i++)
  {
	FILE_LOG(logDEBUG)<<"Neighbor:"<<atoi(tokens[i].c_str());
	  myneighbors.push_back(atoi((tokens[i]).c_str()));
  }
    SubmodelManager::instance().initLp(id,myneighbors,MeName,LPtype,Adapatername);

    return true;

}


CORBA::Boolean smSetup_i::createScheduler(const char* name, CORBA::Double endGVT)
{
    FILE_LOG(logDEBUG)<<"createScheduler called: "<<name;
    FILE_LOG(logDEBUG)<<"createScheduler called: "<<endGVT;
    SubmodelManager::instance().initSched(name, endGVT);


    return true;
}


CORBA::Boolean smSetup_i::initSM(CORBA::ULong id, const char* MH)
{

    FILE_LOG(logDEBUG)<<"initSM "<<id<<"|"<<MH;
    CorbaMessageHandler* cmh=new CorbaMessageHandler();
    SubmodelManager::instance().initSubmodelManager(id,cmh);
}

char* smSetup_i::getSMAddress()
{
    FILE_LOG(logDEBUG)<<"getSMAddress called";
    char* ret=new char[strlen(_siorRun)+1];
    strcpy(ret,_siorRun);
    return ret;


}
void smSetup_i::sendSMAddress(const char* Address)
{
    FILE_LOG(logDEBUG)<<"sendSMAddress called";
}
smSetup_i::smSetup_i(char* siorRun):_siorRun(siorRun)
{

}
smSetup_i::~smSetup_i()
{

}
